package cn.com.oyat.idea.util;

import org.springframework.stereotype.Component;



@Component
public class XmlParserUtil {

    public String extractVideoUrl(String soapRequest) {
        return extractXmlValue(soapRequest, "CurrentURI");
    }

    public String extractVideoTitle(String soapRequest) {
        // 简化实现，实际中可能需要解析DIDL-Lite
        String url = extractVideoUrl(soapRequest);
        if (url != null && !url.isEmpty()) {
            // 从URL中提取可能的标题
            int lastSlash = url.lastIndexOf('/');
            if (lastSlash != -1 && lastSlash < url.length() - 1) {
                String filename = url.substring(lastSlash + 1);
                // 移除扩展名
                int dotIndex = filename.lastIndexOf('.');
                if (dotIndex > 0) {
                    return filename.substring(0, dotIndex);
                }
                return filename;
            }
        }
        return "手机投屏视频";
    }

    private String extractXmlValue(String xml, String tagName) {
        if (xml == null) return null;

        String startTag = "<" + tagName + ">";
        String endTag = "</" + tagName + ">";

        int start = xml.indexOf(startTag);
        if (start == -1) return null;

        start += startTag.length();
        int end = xml.indexOf(endTag, start);
        if (end == -1) return null;

        return xml.substring(start, end).trim();
    }
}